package com.css.fxfzxczh.modules.disasterSituationDataManage.houMonData.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzxczh.base.response.RestResponse;
import com.css.fxfzxczh.modules.disasterSituationDataManage.houMonData.entity.HouMonData;
import com.css.fxfzxczh.modules.disasterSituationDataManage.houMonData.entity.HouMonDataVo;
import com.css.fxfzxczh.modules.disasterSituationDataManage.houMonData.queryParams.HouMonDataQuaParams;
import com.css.fxfzxczh.modules.disasterSituationDataManage.houMonData.service.HouMonDataService;
import com.css.fxfzxczh.modules.disasterSituationDataManage.houMonData.vo.ListVO;
import com.css.fxfzxczh.modules.disasterSituationDataManage.samPoiData.vo.ResultVo;
import com.css.fxfzxczh.util.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author
 * @program
 * @description
 * @create 2022/8/8 10:39
 **/
@RestController
@RequestMapping("/houMonData")
@Slf4j
public class HouMonDataController {
    @Resource
    HouMonDataService houMonDataService;
    /**
     * 分页列表
     * @param queParams
     * @return
     */
    @GetMapping("/getPage")
    public RestResponse getPage(HouMonDataQuaParams queParams, HttpServletRequest request){
        RestResponse restResponse=null;
        JSONObject jsonObject=new JSONObject();
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize=Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String,Object> page=houMonDataService.getPage(queParams,curPage,pageSize);
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(page);
        }catch (Exception e){
            String errormessage="查询失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * App分页列表  只有未提交和已提交
     * @param queParams
     * @return
     */
    @GetMapping("/getAppPage")
    public RestResponse getAppPage(HouMonDataQuaParams queParams, HttpServletRequest request){
        RestResponse restResponse=null;
        JSONObject jsonObject=new JSONObject();
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize=Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String,Object> page=houMonDataService.getAppPage(queParams,curPage,pageSize);
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(page);
        }catch (Exception e){
            String errormessage="查询失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }


    /**
     * 保存
     * @param houMonData
     * @return
     */
    @PostMapping("/save")
    public RestResponse save(@RequestBody HouMonData houMonData){
        RestResponse response = null;
        JSONObject jsonObject=new JSONObject();
        try{
            houMonDataService.save(houMonData);
            jsonObject.put("message","保存成功");
            response = RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errorMessage = "保存失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 保存
     * @param houMonData
     * @return
     */
    @PostMapping("/saveApp")
    public RestResponse saveApp(@RequestBody HouMonData houMonData){
        RestResponse response = null;
        JSONObject jsonObject=new JSONObject();
        try{
            String id=houMonDataService.saveApp(houMonData);
            jsonObject.put("message","保存成功");
            response = RestResponse.succeed(id);
        }catch (Exception e){
            String errorMessage = "保存失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }


    /**
     * 导出
     * @param response
     */
    @PostMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response,@RequestBody HouMonDataQuaParams queParams){
        RestResponse restResponse = null;
        try {
            houMonDataService.exportExcel(response,queParams);
            restResponse = RestResponse.succeed("导出成功");
        } catch (Exception e) {
            String errormessage = e.getMessage();
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        ///return restResponse;
    }

    /**
     * 审核数据导出
     * @param response
     */
    @PostMapping("/checkExportExcel")
    public void checkExportExcel(HttpServletResponse response,@RequestBody HouMonDataQuaParams queParams){
        RestResponse restResponse = null;
        try {
            houMonDataService.checkExportExcel(response,queParams);
            restResponse = RestResponse.succeed("导出成功");
        } catch (Exception e) {
            String errormessage = e.getMessage();
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        ///return restResponse;
    }

    /***
     * 修改
     * @param
     * @return
     */
    @PostMapping("/update")
    public RestResponse updateTask(@RequestBody HouMonData houMonData){
        RestResponse response = null;
        JSONObject jsonObject=new JSONObject();
        try{
            houMonDataService.update(houMonData);
            jsonObject.put("message","修改成功");
            response = RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errorMessage = "修改失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 样本编号
     * @param eventCode
     * @return
     */
    @GetMapping("/getHouMonCode")
    public RestResponse getHouMonCode(String eventCode){
        RestResponse response = null;

        try{
            String str=houMonDataService.getHouMonCode(eventCode);
            response = RestResponse.succeed(str);
        }catch (Exception e){
            String errorMessage = "生成失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 查看
     *
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public RestResponse findById(String id) {
        RestResponse response = null;
        try {
            HouMonDataVo vehManage=houMonDataService.findById(id);
            response = RestResponse.succeed(vehManage);
        } catch (Exception e) {
            String errorMessage = "查看失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 审核（批量审核）
     *
     * @param listvo
     * @return
     */
    @PostMapping("/check")
    public RestResponse check(@RequestBody ListVO listvo) {
        RestResponse response = null;
        try {
            houMonDataService.check(listvo.getList());
        } catch (Exception e) {
            String errorMessage = "审核失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 导入excel
     * @param file
     * @return
     */
    @PostMapping("/importExcel")
    public RestResponse uploadSampling(@RequestParam("file") MultipartFile file, String xsemId) {
        RestResponse response = null;
        try{
            Map<String, Object> map = houMonDataService.importExcel(file,xsemId);
            boolean res = ((Boolean) map.get("res")).booleanValue();;
            if(res){
                response = RestResponse.succeed("上传成功!");
                response.setMessage("上传成功!");
            }else {
                response = RestResponse.fail("上传失败!");
                response.setMessage(map.get("mess").toString());
            }
        }catch (Exception e){
            String errormessage = "上传失败!";
            log.error(errormessage,e);
            response=RestResponse.fail(errormessage);
        }
        return response;
    }

    /***
     * 提交
     * @param
     * @return
     */
    @PostMapping("/submit/{ids}")
    public RestResponse submit(@PathVariable("ids") String ids){
        RestResponse response = null;
        JSONObject jsonObject=new JSONObject();
        try{
            houMonDataService.submit(ids);
            jsonObject.put("message","提交成功");
            response = RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errorMessage = "提交失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /***
     * 提交
     * @param
     * @return
     * App端需要Get 请求
     */
    @GetMapping("/submitApp/{ids}")
    public RestResponse submitApp(@PathVariable("ids") String ids){
        RestResponse response = null;
        JSONObject jsonObject=new JSONObject();
        try{
            houMonDataService.submit(ids);
            jsonObject.put("message","提交成功");
            response = RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errorMessage = "提交失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }


    /***
     * 删除
     * @param
     * @return
     */
    @PostMapping("/delete/{ids}")
    public RestResponse deleteTask(@PathVariable("ids") String ids){
        RestResponse response = null;
        JSONObject jsonObject=new JSONObject();
        try{
            houMonDataService.delete(ids);
            jsonObject.put("message","删除成功");
            response = RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errorMessage = "删除失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /***
     * 删除
     * @param
     * @return
     * App端 需要Get 请求
     */
    @GetMapping("/deleteApp/{ids}")
    public RestResponse deleteApp(@PathVariable("ids") String ids){
        RestResponse response = null;
        JSONObject jsonObject=new JSONObject();
        try{
            houMonDataService.delete(ids);
            jsonObject.put("message","删除成功");
            response = RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errorMessage = "删除失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

}
